﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

using System.Data.SqlClient;

using Business;

namespace Data
{

    public class DataOrganizador
    {
        public DataOrganizador()
        {
        }

        public int insereOrganizador(Organizador org)
        {
            int value = -1;
            Connect c = new Connect();
            try
            {
                SqlConnection conn = c.connect("");
                SqlCommand command = conn.CreateCommand();

                command.CommandText = "INSERT INTO Organizador(nome,email,link,status) VALUES " + " ('" + org.getNomeOrganizador() + "','" + org.getEmailOrganizador() + "','" + org.getLinkOrganizador() + "',1)";
                int i = command.ExecuteNonQuery();

                command.CommandText = "SELECT MAX(idOrganizador) FROM Organizador";
                SqlDataReader reader = command.ExecuteReader();
                if (reader.Read())
                    value = Convert.ToInt16(reader[0]);
                reader.Close();
                conn.Close();
            }
            catch (SqlException ex)
            {
                Console.WriteLine(ex.Message);
            }

            return value;
        }

        public bool setEstadoOrganizador(int organizadorID, int estado)
        {
            bool value = false;
            Connect c = new Connect();
            try
            {
                SqlConnection conn = c.connect("");
                SqlCommand command = conn.CreateCommand();

                command.CommandText = "UPDATE Organizador SET status=" + estado + " WHERE (idOrganizador=" + organizadorID + ")";
                int i = command.ExecuteNonQuery();
                if (i > 0) value = true;
                conn.Close();
            }
            catch (SqlException ex)
            {
                Console.WriteLine(ex.Message);
            }
            return value;
        }

        public Organizador getOrganizadorByID(int id)
        {
            Organizador org = null;
            Connect c = new Connect();
            try
            {
                SqlConnection conn = c.connect("");
                SqlCommand command = conn.CreateCommand();

                command.CommandText = "Select * from Organizador where idOrganizador=" + id;
                SqlDataReader reader = command.ExecuteReader();
                if (reader.Read())
                {
                    org = new Organizador(Convert.ToString(reader[1]), Convert.ToString(reader[2]), Convert.ToString(reader[3]));
                    org.setIDOrganizador(Convert.ToInt32(reader[0]));
                    org.setStatus(Convert.ToInt16(reader[4]));
                }
                reader.Close();
                conn.Close();
            }
            catch (SqlException ex)
            {
                Console.WriteLine(ex.Message);
            }

            return org;
        }

        public bool updateOrganizador(Organizador org)
        {
            bool value = false;
            Connect c = new Connect();
            try
            {
                SqlConnection conn = c.connect("");
                SqlCommand command = conn.CreateCommand();

                command.CommandText = "UPDATE Organizador SET nome='" + org.getNomeOrganizador() + "',email='" + org.getEmailOrganizador() + "',link='" + org.getLinkOrganizador() + "',status=" + org.getStatus() + " WHERE (idOrganizador=" + org.getIDOrganizador() + ")";
                int i = command.ExecuteNonQuery();
                if (i > 0) value = true;
                conn.Close();
            }
            catch (SqlException ex)
            {
                Console.WriteLine(ex.Message);
            }
            return value;
        }
    }


}